using IQIGame.Onigao.Game;
using IQIGame.Onigao.GamePlay;
using System.Collections.Generic;

namespace IQIGame.Onigao.Logic
{
    /// <summary>
    /// 激活或关闭服务单元
    /// 参数：
    /// </summary>
    public class ExecEnableServiceUnit : BaseServiceExecution
    {
        public override void Execute(LevelRegion region, LevelPlayer player)
        {
            ExecParamEnableServiceUnit config = _config as ExecParamEnableServiceUnit;
            if (!config.tagOrMcid.TryGetValue(triggerEntity, out int tagOrMcid))
            {
                LogicLog.LogError("无法获取到tagOrMcid.");
                return;
            }
            int serviceId = config.service;
            int unitIndex = config.serviceUnitIndex;
            bool isEnabled = config.enable;

            #region 不能return的区域，需要先释放modifyEntitys
            List<LevelEntity> modifyEntitys = ListPool<LevelEntity>.Get();
            if (tagOrMcid == 0)
            {
                if (triggerEntity != null)
                {
                    triggerEntity.EnableServiceUnit(serviceId, unitIndex, isEnabled);
                    modifyEntitys.Add(triggerEntity);
                }
            }
            else
            {
                region.GetEntitiesByTagOrMcid(tagOrMcid, modifyEntitys, true);
                foreach (LevelEntity entity in modifyEntitys)
                {
                    entity.EnableServiceUnit(serviceId, unitIndex, isEnabled);
                }
            }

            if (modifyEntitys.Count == 0)
            {
                LogicLog.LogError($"根据TagOrMcid[{tagOrMcid}]找不到任何实体。");
            }
            ListPool<LevelEntity>.Put(modifyEntitys);
            #endregion
        }
    }
}
